//
//  LGSelectGroupChatView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/27.
//

#import "LGSelectGroupChatView.h"
#import "SDImageCache.h"
#import "DCAvatar.h"
@interface LGSelectGroupChatView()<LGEmptyViewDelegate,UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIButton *cancelBtn;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)LGEmptyView *emptyView;
/**   */
@property (nonatomic , copy)NSString *roleType;

@end

@implementation LGSelectGroupChatView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        [self creareSubView];
    }
    return self;
}

-(void)requestData{
    [[SDImageCache sharedImageCache] clearWithCacheType:SDImageCacheTypeAll completion:^{
        [self.tableView reloadData];
    }];
    [RequestUtil withPOST:@"/doctor/my.teams/teamsForUser" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        if(code == 200){
            self.roleType = LGNSString(returnData[@"role_type"]);
            self.dataArray = returnData[@"list"];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:@"暂无团队" offset:0];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:LGEmptyPicName content:LGErrorMessage offset:0];
        }
    }];
}



-(void)showViewAniamtion{
    self.hidden = NO;
    [self requestData];
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        self.baseView.frame = CGRectMake(0, Screen_H-height+15, Screen_W, height);
    }];
}

-(void)cancelButtonAction{
    CGFloat height = self.baseView.bounds.size.height;
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, height);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSelectGroupChatCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.lineView.hidden = (indexPath.row == self.dataArray.count-1)?YES:NO;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self cancelButtonAction];
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    //进入群聊
    if(self.delegate && [self.delegate respondsToSelector:@selector(enterTheGroupChatRoom:title:)]){
        [self.delegate enterTheGroupChatRoom:LGNSString(tempDic[@"chat_group_id"]) title:tempDic[@"title"]];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)creareSubView{
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.cancelBtn];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.tableView];
    [self.baseView addSubview:self.emptyView];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(487))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UIButton *)cancelBtn{
    if(!_cancelBtn){
        _cancelBtn = [UIButton buttonWithImage:@"弹框关闭" selectImage:@"弹框关闭"];
        _cancelBtn.frame = CGRectMake(Screen_W-viewPix(60), viewPix(15), viewPix(48), viewPix(30));
        [_cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelBtn;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"发起群聊" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightSemibold) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(viewPix(100), viewPix(15), Screen_W-viewPix(200), viewPix(30));
    }
    return _titleLabel;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(60), Screen_W, viewPix(427)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGSelectGroupChatCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.rowHeight = viewPix(60);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(20))];
    }
    return _tableView;
}


-(LGEmptyView *)emptyView{
    if(!_emptyView){
        _emptyView = [[LGEmptyView alloc]initWithFrame:self.tableView.frame];
        _emptyView.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
        _emptyView.margin = viewPix(15);
        _emptyView.needBuffer = YES;
        _emptyView.delegate = self;
    }
    return _emptyView;
}

@end



@interface LGSelectGroupChatCell()


/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UILabel *myTeamLabel;

@end

@implementation LGSelectGroupChatCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *title = [NSString stringWithFormat:@"%@（%@）",dataDic[@"title"],dataDic[@"nums"]];
    [self.iconView dc_setImageAvatarWithGroupId:LGNSString(dataDic[@"teamid"]) Source:dataDic[@"iconAry"]];
    if([dataDic[@"isleader"] integerValue] == 1){
        //我的团队
        self.myTeamLabel.hidden = NO;
        title = (title.length>8)?[NSString stringWithFormat:@"%@...",[title substringToIndex:8]]:title;
        
    }else{
        self.myTeamLabel.hidden = YES;
        title = (title.length>15)?[NSString stringWithFormat:@"%@...",[title substringToIndex:15]]:title;
    }
    self.titleLabel.text = title;
}


-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor colorWithString:@"#FFFFFF"];
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.myTeamLabel];
    [self.contentView addSubview:self.lineView];
 
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(20));
        make.width.height.equalTo(@(viewPix(42)));
        make.centerY.equalTo(self.contentView);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(viewPix(20));
        make.centerY.equalTo(self.contentView);
    }];
    [self.myTeamLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(viewPix(12));
        make.centerY.equalTo(self.contentView);
        make.width.equalTo(@(viewPix(72)));
        make.height.equalTo(@(viewPix(24)));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView);
        make.right.equalTo(self.contentView).offset(-viewPix(20));
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@(0.5));
    }];
}

-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]init];
        _iconView.backgroundColor = [UIColor colorWithString:@"#EEEEEE"];
        _iconView.cornerRidus = 5;
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UILabel *)myTeamLabel{
    if(!_myTeamLabel){
        _myTeamLabel = [UILabel lableWithText:@"我的团队" colorString:@"#27B6D7" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _myTeamLabel.backgroundColor = [UIColor colorWithString:@"#ECF7FF"];
        _myTeamLabel.cornerRidus = 3;
    }
    return _myTeamLabel;
}
-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E1E1E1"];
    }
    return _lineView;
}

@end
